<template>
	<view class="u-page" v-if="data">
		<u-notice-bar fontSize="13" :text="data.loop_txt"></u-notice-bar>
		<view class="loopimg">
			<u-swiper
				:list="data.loop_img" 
				:indicator="true" 
				height="150"
				@click="clickImg"
				:autoplay="true"
				bgColor="#ffffff"
				radius="0"
			>
			</u-swiper>
		</view>
		<view class="apply" v-if="!is_customer" style="background-color: #f9ae3d;">
			<view class="title">提醒：</view>
			<view class="content" v-if="!is_customer && !register_id">
				您的微信没有绑定任何用户,<text @click="goRegister" style="color: #c55252;">点击这里</text>申请入驻!
			</view>
			<view class="content" v-if="!is_customer && register_id">
				您申请的用户正在审核中,<text @click="goRegister" style="color: #c55252;">点击这里</text>修改申请!
			</view>
		</view>
		<view class="apply" v-if="is_customer && customer" style="background-color: #53bff9;">
			<view class="content">
				服务商：{{customer.channel.title}}
			</view>
			<view class="content" style="margin-top: 10rpx;display: flex;justify-content: space-between;">
				<text>当前用户：{{customer.name}}</text>
				<text class="under" @click="showChange=true" v-if="customers.length>1">切换</text>
			</view>
		</view>
		<view class="menubox">
			<view class="item" v-if="is_customer" @click="$navigateTo" data-url="index/user">
				<view class="title">
				<image src="/static/icon/user.png"></image>
				<text>会员中心</text>
				</view>
			</view>
			<view class="item" @click="$navigateTo" data-url="admin/index" v-if="is_admin || is_seller">
				<view class="title">
				<image src="/static/icon/admin.png"></image>
				<text>管理员</text>
				</view>
			</view>
			<view class="item" @click="$navigateTo" data-url="driver/today" v-if="is_driver">
				<view class="title">
				<image src="/static/icon/driver.png"></image>
				<text>司机</text>
				</view>
			</view>
			<view class="item" v-if="is_customer" @click="$navigateTo" data-url="shop/index">
				<view class="title">
				<image src="/static/icon/shop.png"></image>
				<text>积分商城</text>
				</view>
			</view>
			<block v-if="is_customer && customer">
			<view v-for="(goods,index) in customer.channel.goods" :key="index" class="item" @click="goGoods(goods.id)">
				<view class="title">
				<image :src="goods.icon"></image>
				<text>{{goods.name}}</text>
				</view>
			</view>
			</block>
		</view>
		<view class="aboutus">
			<view class="title">关于我们</view>
			<view class="content">
				{{data.about_us}}
			</view>
		</view>
		<view class="kefu">
			<!--#ifdef H5-->
			<button @click="call">
				<image src="/static/icon/kefu.png"></image>
			</button>
			<!--#endif-->
			<!--#ifdef MP-WEIXIN-->
			<button open-type="contact">
				<image src="/static/icon/kefu.png"></image>
			</button>
			<!--#endif-->
			<view>客服</view>
		</view>
		<u-popup :show="showChange" @close="showChange=false" mode="center" :round="4" customStyle="width:90%;padding:20rpx 0;">
			<view class="changeStore">
				<view class="item" v-for="(item,index) in customers" :key="index">
					<view class="ibox">
						<text>服务商：{{item.channel.title}}</text>
						<text>用户：{{item.name}}</text>
					</view>
					<u-button v-if="customer.id!=item.id" type="primary" size="mini" @click="changeStore(item)">切换</u-button>
				</view>
			</view>
		</u-popup>
		<privacy-popup ref="privacyComponent"  @agree-privacy="onAgreePrivacy"></privacy-popup>
	</view>
</template>

<script>
	import {baseUrl,methods} from '@/utils/methods.js';
	import * as privacyPopup from '@/pages/components/privacy-popup.vue';
	export default {
		components:{
			privacyPopup
		},
		data() {
			return {
				baseUrl:'',
				data:'',
				is_admin:'',
				is_driver:'',
				is_seller:'',
				is_customer:'',
				customer:'',
				customers:[],
				register_id:'',
				showChange:false
			}
		},
		onPullDownRefresh() {
			this.getAuth(true);
		},
		onLoad() {
			this.baseUrl=baseUrl;
			this.$get('index/siteinfo').then(res=>{
				this.data=res;
			});
			let userinfo=this.$userinfo() || 0;
			if(userinfo){
				this.getAuth();
			}
		},
		methods: {
			...methods,
			onAgreePrivacy:function(){
				this.getAuth();
			},
			call:function(){
				uni.makePhoneCall({
					phoneNumber:this.customer.channel.phone
				});
			},
			getAuth:function(downrefresh){
				this.$get('user/auth').then(res=>{
					this.is_admin=res.is_admin;
					this.is_driver=res.is_driver;
					this.is_seller=res.is_seller;
					this.is_customer=res.is_customer;
					this.is_register=res.is_register;
					this.register_id=res.register_id;		
					let customer='';
					if(res.customer.length==1){
						customer=res.customer[0];
					}
					if(res.customer.length>1){
						customer=uni.getStorageSync('customer') || '';
						if(customer){
							for(let i=0;i<res.customer.length;i++){
								if(res.customer[i].id==customer.id){
									customer=res.customer[i];
									break;
								}
							}
						}
						if(!customer){
							customer=res.customer[0];
						}
					}
					uni.setStorageSync('customer',customer);
					this.customer=customer;
					this.customers=res.customer;
					if(downrefresh){
						uni.stopPullDownRefresh();
					}
				});
			},
			changeStore:function(item){
				uni.setStorageSync('customer',item);
				this.customer=item;
				this.showChange=false;
			},
			goXunjia:function(id){
				this.$navigateTo('buy/xunjia?goods_id='+id);
			},
			clickImg:function(e){
				uni.previewImage({
					urls:this.data.loop_img,
					current:e
				});
			},
			goGoods:function(id){
				let goods=this.customer.goods;
				for(let i=0;i<goods.length;i++){
					if(goods[i].goods_id==id){
						this.$navigateTo('buy/index?goods_id='+id);
						return;
					}
				}
				this.$msg('该商品尚未设置价格，请联系服务商');
			},
			goRegister:function(){
				let id=this.register_id || '';
				this.$navigateTo('index/register?register_id='+id);
			},
			changeTowel:function(e){
				this.$post('store/index/changeChannel',{admin_id:e.admin_id},true).then(res=>{
					this.goTowel();	
				});
			},
			goTowel:function(e){
				if(e){
					this.showTowel=false;
					uni.setStorageSync('towel',e);	
					this.navigateTo('store/towel/route');
					return;
				}
				this.$get('store/index/towel','',true).then(res=>{
					if(res.length>1){
						this.towel=res;
						this.showTowel=true;
					}else{
						uni.setStorageSync('towel',res[0]);
						this.navigateTo('store/towel/route');
					}
				});
			}
		}
	} 
</script>

<style lang="scss">
	.apply{
		margin:0 auto;
		width: 92%;
		padding: 20rpx 0;		
		border-radius: 8rpx;
		color: #fff;
		.title{
			font-weight: bold;
			font-size: 28rpx;
			padding: 0 20rpx;
		}
		.content{
			font-size: 28rpx;
			padding: 0 20rpx;
		}
		.under{
			text-decoration: underline;
			margin-left: 30rpx;
		}
	}
	.loopimg{
		margin-bottom: 40rpx;
	}
	.menubox{
		display: grid;
		grid-template-columns: 30% 30% 30%;
		width: 92%;
		margin: 20rpx auto;
		grid-column-gap: 5%;
		.item{
			border: 1px solid #eaeaea;
			margin-bottom: 40rpx;
			padding:20rpx 0;
			padding-bottom: 10rpx;
			position: relative;
			.title{
				display: flex;
				flex-direction: column;
				align-items: center;
				font-size: 26rpx;
				text{
					margin:10rpx 0;
				}
			}
			image{
				width:90rpx;
				height:90rpx;
			}
			.tips{
				position: absolute;
				right: 10rpx;
				top: 10rpx;
				border-radius: 50%;
				width: 40rpx;
				height: 40rpx;
				line-height: 40rpx;
				text-align: center;
				font-size: 26rpx;
				background-color: #e82e2e;
				color: #fff;
			}
		}
	}
	.kefu{
		position: fixed;
		bottom: 20rpx;
		right: 20rpx;
		display: flex;
		flex-direction: column;
		font-size: 28rpx;
		color: #4f4f4f;
		font-weight: bold;
		align-items: center;
		image{
			width: 80rpx;
			height: 80rpx;
		}
		button{
			height: 80rpx;
			border: 0;
			background-color: #fff;
			padding: 0;
		}
		button::after{
			border: 0;
		}
	}
	.aboutus{
		padding: 30rpx;
		padding-top: 0;
		.title{
			font-weight: bold;
		}
		.content{
			padding-top: 20rpx;
			line-height: 46rpx;
		}
	}
	.changeStore{
		padding: 30rpx 30rpx 0 30rpx;
		.item{
			display: flex;
			justify-content: space-between;
			font-size: 28rpx;
			margin-bottom: 20rpx;
			padding-bottom: 20rpx;
			border-bottom: 1px solid #eaeaea;
			align-items: center;
			.ibox{
				display: flex;
				flex-direction: column;
				height: 80rpx;
				justify-content: space-around;
				width: 80%;
			}
			button{
				width: 100rpx;
			}
		}
		.item:last-child{
			margin-bottom: 0;
			padding-bottom: 0;
			border-bottom: 0;
		}
	}
</style>